package com.example.litterpainterdemo.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.view.View
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object BitmapUtils {
    //View -> Bitmap
    suspend fun convertViewToBitmap(view:View):Bitmap = withContext(Dispatchers.IO){
        //创建自己的bitmap
        val bitmap = Bitmap.createBitmap(view.width,view.height,Bitmap.Config.ARGB_8888)
        //将view的图绘制到自己的bitmap
        val canvas = Canvas(bitmap)
        canvas.drawColor(Color.parseColor("#1A1A1A"))
        view.draw(canvas)
        bitmap
    }

    //缩略图
    suspend fun convertThumbnail(bitmap: Bitmap,scale:Float):Bitmap = withContext(Dispatchers.IO){
        Bitmap.createScaledBitmap(
            bitmap,
            (bitmap.width*scale).toInt(),
            (bitmap.height*scale).toInt(),
            true
        )
    }

    //加载原图
    suspend fun loadOriginalDrawing(path:String):Bitmap = withContext(Dispatchers.IO){
        BitmapFactory.decodeFile(path)
    }
}